<template>
  <div>
    <el-dialog :title="title" :visible.sync="examineShow" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="意见:">
          <el-input
            type="textarea"
            style="width: 100%"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入意见"
            v-model="form.comment"
          >
          </el-input>
        </el-form-item>
        <!-- <el-form-item label="附件">
          <el-upload class="upload-demo" :file-list="form.fileList" drag
            action="https://jsonplaceholder.typicode.com/posts/" multiple>
            <i class="el-icon-upload" style="color: #C0C4CC;"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="btnLoading" type="primary" @click="examineSubmit(false)">确 定</el-button>
        <el-button :loading="btnLoading" @click="examineShow = false">取 消</el-button>
      </div>
    </el-dialog>
    <action-button
      v-if="taskName"
      :taskName="taskName"
      :loading="btnLoading"
      :showClose="true"
      @changeExamineShow="changeExamineShow"
      @openClose="openClose"
    />
  </div>
</template>

<script>
import { auditchange } from "@/api/resources/apply.js";
import { uploadFile } from "@/api/system/upload.js";
import actionButton from "@/components/actionButton/index.vue";
// import { delete } from 'vue/types/umd';
export default {
  name: "buttons",
  components: { actionButton },
  props: {
    taskId: {
      type: String,
      default() {
        return 0;
      },
    },
    taskName: {
      type: String,
      default() {
        return "";
      },
    },
    detail: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      //按钮loading
      btnLoading: false,
      //弹窗标题
      title: "",
      // 审核弹窗
      examineShow: false,
      //角色列表
      role: [],
      //表单数据
      form: {
        // 意见
        comment: this.title,
        //文件
        fileList: [],
      },
      supplierForm: {
        supplierRemark: "",
        supplierFiles: [],
      },
      xnum: 0,
    };
  },
  created() {},
  methods: {
    submit() {
      this.$confirm("确定要提交审核吗？", "提示", {
        type: "warning",
      }).then(() => {
        this.form = {
          comment: this.title,
          fileList: [],
        };
        this.examineSubmit(4);
      });
    },
    //关闭外层弹窗
    openClose() {
      // this.$emit('openClose', false)
      this.$emit("openClose", false);
    },
    //显示审核弹窗
    changeExamineShow(num, title) {
      this.form = {
        comment: title,
        fileList: [],
      };
      this.title = title;
      if (num == 4) {
        this.submit();
      } else {
        this.examineShow = num;
      }
    },
    //审核提交
    examineSubmit(num, fileList) {
      this.btnLoading = true;

      if (num == -1) {
        num = this.xnum;
      } else {
        this.xnum = num;
      }

      if (this.taskName == "资源变更") {
        if (fileList == undefined) {
          this.$emit("fileUp");
          return;
        } else if (!fileList) {
          this.btnLoading = false;
          return;
        }
      }

      // this.detail.hosts.forEach(item=>{
      //   console.log(item,'云主机')
      //   delete item.changeTo.id
      // })
      // this.detail.databases.forEach(item=>{
      //   console.log(item,'数据库')
      //   delete item.changeTo.id
      // })
      // this.detail.storeProducts.forEach(item=>{
      //   console.log(item,'存储产品')
      //   delete item.changeTo.id
      // })
      // this.detail.safeProducts.forEach(item=>{
      //   console.log(item,'安全产品')
      //   delete item.changeTo.id
      // })

      let flag = true;
      if (this.title == "派发") {
        this.$refs.form.validate((valid) => {
          flag = valid;
        });
      }
      if (!flag) {
        return;
      }

      let examineShow = num || this.examineShow;
      let variables = {
        actionName_: this.title,
      };
      if (this.taskName == "云服务商处理") {
        let supplierFiles = new FormData();
        supplierFiles.append("key", "file");
        this.detail.supplierFiles.forEach((item) => {
          if (item.raw) {
            supplierFiles.append("file", item.raw);
          }
        });
        if (supplierFiles.get("file")) {
          uploadFile(supplierFiles).then((res) => {
            this.detail.supplierFiles = res.data;
            let detail = JSON.parse(JSON.stringify(this.detail));
            let safeProducts = [];
            detail.safeProductsList.forEach((item) => {
              let num = 0;
              detail.safeProducts.forEach((it) => {
                if (it.productName == item.name) {
                  if (num < item.num) {
                    delete it.changeTo.insStatus;
                  }
                  safeProducts.push(it);
                  num++;
                }
              });
            });

            detail.safeProducts = safeProducts;

            auditchange({
              taskId: this.taskId,
              comment: this.form.comment,
              actionName: this.title,
              variables,
              action: examineShow != 2 ? "COMPLETE" : this.taskName == "监管平台接收" ? "REJECT" : "BACK",
              formObject: detail,
            })
              .then((response) => {
                this.$message({
                  message: response.msg,
                  type: "success",
                });
                this.examineShow = false;
                this.$emit("openClose", true);
              })
              .finally(() => {
                this.btnLoading = false;
              });
          });
          return;
        }
      }
      let detail = JSON.parse(JSON.stringify(this.detail));

      let safeProducts = [];
      detail.safeProductsList.forEach((item) => {
        let num = 0;
        detail.safeProducts.forEach((it) => {
          if (it.productName == item.name) {
            if (num < item.num && it.changeTo) {
              delete it.changeTo.insStatus;
            }
            safeProducts.push(it);
            num++;
          }
        });
      });
      detail.safeProducts = safeProducts;
      if (fileList) {
        detail.files = detail.files.concat(fileList);
      }
      // 去除为空的文件
      if (detail.files.length > 0 && detail.files != undefined) {
        for (var i = 0; i < detail.files.length; i++) {
          if (detail.files[i] == undefined) {
            detail.files.splice(i, 1);
            i--;
          }
        }
      }
      auditchange({
        taskId: this.taskId,
        comment: this.form.comment,
        actionName: this.title,
        variables,
        action: examineShow != 2 ? "COMPLETE" : this.taskName == "监管平台接收" ? "REJECT" : "BACK",
        formObject: detail,
      })
        .then((response) => {
          this.$message({
            message: response.msg,
            type: "success",
          });
          this.examineShow = false;
          this.$emit("openClose", true);
        })
        .finally(() => {
          this.btnLoading = false;
        });
    },
  },
};
</script>

<style scoped lang="less"></style>
